//
//  BVyue.m
//  BuyVegetable
//
//  Created by zhuzi on 16/10/21.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVBalanceHeandView.h"

@implementation BVBalanceHeandView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithTitelArray:(NSArray *)array{
    
    self = [super init];
    
    if (self) {
        
        _titlesButtonArray = [NSMutableArray arrayWithCapacity:0];
        self.backgroundColor = [UIColor whiteColor];
        _arryTitel = [[NSMutableArray alloc]init];
        [_arryTitel addObjectsFromArray:array];
        
        NSInteger a = ScreenW-ScreenW/(_arryTitel.count+1)*(_arryTitel.count);
        NSInteger b = a/(_arryTitel.count+1);
        
        for (NSInteger i = 0; i<3; i++) {
            UIButton * but = [UIButton buttonWithType:UIButtonTypeSystem];
            [but setTitle:[NSString stringWithFormat:@"%@",_arryTitel[i]] forState:UIControlStateNormal];
            [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [but addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            but.tag = i+1;
            [self addSubview:but];
            but.sd_layout.topSpaceToView(self,0).leftSpaceToView(self,(i+1)*b+(i*ScreenW/4)).widthIs(ScreenW/4).heightIs(40);
            [self.titlesButtonArray addObject:but];
            if (i == 0) {
                
                [self titleButtonClick:but];
            }
            if (i < _arryTitel.count-1) {
                UIView *  verticalView = [[UIView alloc]init];
                verticalView.backgroundColor = [UIColor blackColor];
                [self addSubview:verticalView];
                verticalView.sd_layout.topSpaceToView(self,5).leftSpaceToView(but,b/2).widthIs(1).heightIs(30);
            }
        }
        //下方小条
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(b, 37, ScreenW/4, 2)];
        _lineView.backgroundColor = [UIColor BVBaseOrangeColor];
        [self addSubview:_lineView];
        
        
    }
    return self;
}
#pragma mark but点击事件
- (void)titleButtonClick:(UIButton *)button {
    
    NSUInteger index = [self.titlesButtonArray indexOfObject:button];
    //1.改变颜色
    [self.currentTitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor BVBaseOrangeColor] forState:UIControlStateNormal];
    [self updateTitleWithIndex:index];
    self.currentTitleButton = button;
    //代理传
    [_delegate getIndex:(NSInteger*)index];
}
#pragma mark 更新title显示
- (void)updateTitleWithIndex:(NSUInteger)index  {
    
    UIButton *button = self.titlesButtonArray[index];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    _lineView.centerX = button.centerX;
    [UIView commitAnimations];
    
    

}

@end
